Developer Documentation

QuickTime 4 API Documentation

Inside Macintosh: Macintosh Toolbox Essentials

Previous | Chapter Top | Chapter Contents | Next |

Reading the Keyboard

The Event Manager reports keyboard events one at a time at your application's request when you use the WaitNextEvent (WaitNextEvent) , EventAvail (EventAvail) , or GetNextEvent (GetNextEvent) function. In addition to getting keyboard events when the user presses or releases a key, you can directly read the keyboard (and keypad) at any time using the GetKeys function (GetKeys) .

You can also use the KeyTranslate function (KeyTranslate) to convert virtual key codes to character code values using a specified ' KCHR ' resource.

GetKeys

You can use the GetKeys function to obtain the current state of the keyboard.

pascal void GetKeys (KeyMap theKeys);
theKeys
Returns the current state of the keyboard, including the keypad, if any. The GetKeys function returns this information using the KeyMap type.
typedef long KeyMap[4];
The KeyMap type is interpreted as an array of 128 elements, each having a Boolean value. Each key on the keyboard or keypad corresponds to an element in the KeyMap array. The index for a particular key is the same as the key's virtual key code minus 1. For example, the key with virtual key code 38 (the "J" key on the Apple Keyboard II) is the 38th element in the returned array. A KeyMap element is true if the corresponding key is down and false if it isn't. The maximum number of keys that can be down simultaneously is two character keys plus any combination of the five modifier keys.

DESCRIPTION

You can use the GetKeys function to determine the current state of the keyboard at any time. For example, you can determine whether one of the modifier keys is down by itself or in combination with another key using the GetKeys function.

KeyTranslate

You can use the KeyTranslate function to convert a virtual key code to a character code based on a 'KCHR' resource. The KeyTranslate function is also available as the KeyTrans function.

pascal long KeyTranslate (const void *transData,
                                         UInt16 keycode,
                                         UInt32 *state);
transData
A pointer to the ' KCHR ' resource that you want the KeyTranslate function to use when converting the key code to a character code.
keycode
A 16-bit value that your application should set so that bits 0-6 contain the virtual key code and bit 7 contains either 1 to indicate an up stroke or 0 to indicate a down stroke of the key. Bits 8-15 have the same interpretation as the high byte of the modifiers field of the event record and should be set according to the needs of your application.
state
A pointer to a value that your application should set to 0 the first time it calls KeyTranslate or any time your application calls KeyTranslate with a different 'KCHR' resource. Thereafter, your application should pass the same value in the state parameter as KeyTranslate returned in the previous call.

DESCRIPTION

The KeyTranslate function returns a 32-bit value that gives the character code for the virtual key code specified by the keycode parameter. Figure 1 shows the structure of the 32-bit number that KeyTranslate returns.

Figure 1 Structure of the KeyTranslate function result

The KeyTranslate function returns the values that correspond to one or possibly two characters that are generated by the specified virtual key code. For example, a given virtual key code might correspond to an alphabetic character with a separate accent character. For example, when the user presses Option-E followed by N, you can map this through the KeyTranslate function using the U.S. 'KCHR' resource to produce ´n, which KeyTranslate returns as two characters in the bytes labeled Character code 1 and Character code 2. If KeyTranslate returns only one character code, it is always in the byte labeled Character code 2. However, your application should always check both bytes labeled Character code 1 and Character code 2 in Figure 1 for possible values that map to the virtual key code.

SEE ALSO

For additional information on the 'KCHR' resource and the KeyTranslate function, see Inside Macintosh: Text .


© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next